package com.wpam.scrabble.mobile.ui;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ColorFilter;
import android.graphics.Paint;
import android.graphics.Paint.Align;
import android.graphics.RectF;
import android.graphics.Typeface;
import android.graphics.Paint.Style;
import android.graphics.drawable.Drawable;

public class ButtonDrawable extends Drawable {

	private Paint buttonPaint_;
	private Paint textPaint_;
	private String text_;
	private int topLeftX_;
	private int topLeftY_;
	private int bottomRightX_;
	private int bottomRightY_;
	private int textCenterX_;
	private int textCenterY_;
	private RectF rect_;
	private int bounds_;
	
	public ButtonDrawable(int posX, int posY, int sizeX, int sizeY, String value) {
		
		this.topLeftX_ = posX;
		this.topLeftY_ = posY;
		this.bottomRightX_ = posX + sizeX;
		this.bottomRightY_ = posY + sizeY;
		this.textCenterX_ = posX + sizeX/2;
		this.textCenterY_ = (int) (posY + sizeY*0.66);
		this.rect_ = new RectF((float)topLeftX_, (float)topLeftY_, (float)bottomRightX_, (float)bottomRightY_);
		this.bounds_ = sizeY / 4;
		this.text_ = value;
		this.buttonPaint_ = new Paint();
		this.buttonPaint_.setColor(Color.rgb(48, 85, 97));
		this.textPaint_ = new Paint();
		this.textPaint_.setTextSize(14);
		this.textPaint_.setStyle(Style.FILL);
		this.textPaint_.setAntiAlias(true);
		this.textPaint_.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
		this.textPaint_.setTextAlign(Align.CENTER);
		this.textPaint_.setColor(Color.WHITE);
	}

	public boolean amIHit(int x, int y) {
		
		if (x >= topLeftX_ && x <= bottomRightX_ 
				&& y >= topLeftY_ && y <= bottomRightY_)
			return true;
		return false;
	}
	
	@Override
	public void draw(Canvas canvas) {
		
		canvas.drawRoundRect(rect_, bounds_, bounds_, buttonPaint_);
		canvas.drawText(text_, textCenterX_, textCenterY_, textPaint_);
	}

	@Override
	public int getOpacity() {
		return 0;
	}

	@Override
	public void setAlpha(int alpha) {

		buttonPaint_.setAlpha(alpha);
		textPaint_.setAlpha(alpha);
	}

	@Override
	public void setColorFilter(ColorFilter cf) {

		buttonPaint_.setColorFilter(cf);
		textPaint_.setColorFilter(cf);
	}

	public void onResize(int posX, int posY, int sizeX, int sizeY) {

		this.topLeftX_ = posX;
		this.topLeftY_ = posY;
		this.bottomRightX_ = posX + sizeX;
		this.bottomRightY_ = posY + sizeY;
		this.textCenterX_ = posX + sizeX/2;
		this.textCenterY_ = (int) (posY + sizeY * 0.66);
		this.rect_.set((float)topLeftX_, (float)topLeftY_, (float)bottomRightX_, (float)bottomRightY_);
		this.bounds_ = sizeY / 4;
	}

}
